package com.xjtu.service.impl;

import com.xjtu.mapper.ElderlyCareRecordMapper;
import com.xjtu.result.Result;
import com.xjtu.service.ReportService;
import com.xjtu.vo.Report.RecordData;
import com.xjtu.vo.Report.ReportVo;
import com.xjtu.vo.Report.Series;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class ReportServiceImpl implements ReportService {
    @Autowired(required = false)
    ElderlyCareRecordMapper elderlyCareRecordMapper;

    public List<LocalDate> initTime(){
        int days = 5;

        LocalDate end =LocalDate.now();

        List<LocalDate> timeList = new ArrayList<LocalDate>();
        for(int i =days; i > 0; i--){
            timeList.add(end.minus(i-1, ChronoUnit.DAYS));
        }
        System.out.println(timeList);
        return timeList;
    }
    public ReportVo init(String name, String color, List<LocalDate> timeList, List<Double> dataList) {


        Series series = new Series();
        series.setName(name);
        series.setColor(color);
        series.setData(new ArrayList<>());
        series.setData(dataList);

        ReportVo reportVo = new ReportVo();
        reportVo.getSeries().add(series);
        reportVo.getXAxis().setData(timeList);
        reportVo.getLegend().getData().add(name);
        return reportVo;
    }


    @Override
    public Result getTemperature(int elderlyId) {

        List<LocalDate> timeList = initTime();
        List<Double> list = new ArrayList<>();

        for(LocalDate date : timeList) {
            // 查询date日期下的营业额数据
            //            // select sum(acount) from orders where order_time > ? and order_time < ? and status = 5
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap();
            map.put("begin", beginTime);
            map.put("end", endTime);
            map.put("elderlyId", elderlyId);
            RecordData recordData = elderlyCareRecordMapper.getByTimeMap(map);
            if (recordData == null) {
                list.add(0.0);
            } else {
                list.add(recordData.getBodyTemperature());
            }

        }
        ReportVo reportVo = init("体温","red", timeList, list);
        return Result.success(reportVo);
    }

    @Override
    public Result getPulseRate(int elderlyId) {
        List<LocalDate> timeList = initTime();
        List<Double> list = new ArrayList<>();

        for(LocalDate date : timeList) {
            // 查询date日期下的营业额数据
            //            // select sum(acount) from orders where order_time > ? and order_time < ? and status = 5
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap();
            map.put("begin", beginTime);
            map.put("end", endTime);
            map.put("elderlyId", elderlyId);
            RecordData recordData = elderlyCareRecordMapper.getByTimeMap(map);
            if (recordData == null) {
                list.add(0.0);
            } else {
                list.add((double) recordData.getPulseRate());
            }

        }
        ReportVo reportVo = init("心率","blue", timeList, list);
        return Result.success(reportVo);
    }
}
